﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;

namespace QuanLyNhapXuat.Presentation.WinForm
{
    public partial class frm_SaoLuu_PhucHoiDuLieu : Telerik.WinControls.UI.RadForm
    {
        private static frm_SaoLuu_PhucHoiDuLieu doituong = null;
        SR_XL_TaiKhoan.WS_TaiKhoanSoapClient xl_DULIEU = new QuanLyNhapXuat.SR_XL_TaiKhoan.WS_TaiKhoanSoapClient();
        SR_XL_TaiKhoan.WS_TaiKhoanSoapClient taikhoan = new QuanLyNhapXuat.SR_XL_TaiKhoan.WS_TaiKhoanSoapClient();
        public static frm_SaoLuu_PhucHoiDuLieu TaoDoiTuong()
        {
            if (doituong == null)
                doituong = new frm_SaoLuu_PhucHoiDuLieu();
            return doituong;
        }
        public frm_SaoLuu_PhucHoiDuLieu()
        {
            InitializeComponent();
        }


        private void radButton1_Click(object sender, EventArgs e)
        {
            if (rdb_SaoLuu.IsChecked == true)
            {
                if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                {
                    txt_DuongDan.Text = folderBrowserDialog1.SelectedPath;
                }
            }
            else
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        txt_DuongDan.Text = openFileDialog1.FileName;
                    }
            }
        }

        private void btn_DongY_Click(object sender, EventArgs e)
        {
            if (rdb_SaoLuu.IsChecked == true)
            {
                try
                {
                    string chuoi_NgayThang = DateTime.Now.Day.ToString("00") + DateTime.Now.Month.ToString("00") + DateTime.Now.Year.ToString();
                    radWaitingBar1.StartWaiting();
                    xl_DULIEU.SelectSQL("backup database QLNX to disk = '" + txt_DuongDan.Text.Trim() + "\\" + chuoi_NgayThang + "_QLNX.bak'");                    
                    MessageBox.Show("Sao lưu dữ liệu thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    radWaitingBar1.ResetWaiting();
                    radWaitingBar1.StopWaiting();
                }
                catch (Exception)
                {
                    MessageBox.Show("Dữ liệu chưa được sao lưu", "Thông Báo");
                    radWaitingBar1.ResetWaiting();
                    radWaitingBar1.StopWaiting();
                }                
            }
            else
            {
                try
                {
                    radWaitingBar1.StartWaiting();
                    string duong_dan = txt_DuongDan.Text;
                    taikhoan.Restore_Data("Alter database QLNX Set MULTI_USER WITH ROLLBACK IMMEDIATE "+
                                        "restore database QLNX from disk = '" + duong_dan + "' with replace");                                        
                }catch (Exception ex){}
                
                radWaitingBar1.ResetWaiting();
                radWaitingBar1.StopWaiting();
                MessageBox.Show("Phục hồi dữ liệu thành công.");                
            }
        }

        private void frm_SaoLuu_PhucHoiDuLieu_Resize(object sender, EventArgs e)
        {
           this.Width = 362;
           this.Height = 272;
        }

        private void btn_HuyBo_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frm_SaoLuu_PhucHoiDuLieu_Load(object sender, EventArgs e)
        {            
        }

        
    }
}
